package edu.hawaii.ics466;

import java.util.concurrent.TimeUnit;

import android.app.ActionBar;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.hawaii.ics466.objects.GameInfo;
import edu.hawaii.ics466.objects.Review;
import edu.hawaii.ics466.tasks.GetGameInfo;
import edu.hawaii.ics466.tasks.GetReviews;
import edu.hawaii.ics466.views.ReviewDialog;
import edu.hawaii.ics466.views.ReviewView;

public class GameView extends Activity {

	public static final String INTENT_KEY_GAME_NAME = "game_name";

	private GameInfo gameInfo;
	private Review[] reviews;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		String gameName = getIntent().getStringExtra(INTENT_KEY_GAME_NAME);
		String[] requestParams = { gameName };
		try {
			gameInfo = new GetGameInfo().execute(requestParams).get(3, TimeUnit.SECONDS);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (gameInfo != null) {
			setupLayout(gameInfo);
			addReviews(gameInfo.getName());
			registerReviewButton();
		} else {
			Toast.makeText(this, "failed to get game info", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
		}
		return true;
	}

	private void setupLayout(GameInfo gameInfo) {
		setContentView(R.layout.game_view);

		// fill content
		TextView gameName = (TextView) findViewById(R.id.game_name);
		gameName.setText(gameInfo.getName());

		TextView desc = (TextView) findViewById(R.id.game_description);
		desc.setText(gameInfo.getDescription());

		TextView ignRating = (TextView) findViewById(R.id.ign_rating_text);
		ignRating.setText("" + gameInfo.getIGNRating());

		TextView metaRating = (TextView) findViewById(R.id.metacritic_rating_text);
		metaRating.setText("" + gameInfo.getMetacriticRating());

		// clickable layouts
		RelativeLayout ignLayout = (RelativeLayout) findViewById(R.id.ign_rating_layout);
		ignLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ign.com/"));
				startActivity(browser);
			}
		});

		RelativeLayout metaLayout = (RelativeLayout) findViewById(R.id.metacritic_rating_layout);
		metaLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.metacritic.com/"));
				startActivity(browser);
			}
		});
	}

	private void addReviews(String gameName) {
		String[] requestParams = { gameName };
		try {
			reviews = new GetReviews().execute(requestParams).get(3, TimeUnit.SECONDS);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (reviews != null) {
			LinearLayout reviewList = (LinearLayout) findViewById(R.id.review_list);
			for (int i = 0; i < reviews.length; i++) {
				Review review = reviews[i];
				ReviewView reviewView = new ReviewView(this);

				TextView username = (TextView) reviewView.findViewById(R.id.username);
				username.setText(review.getUsername());

				TextView date = (TextView) reviewView.findViewById(R.id.review_date);
				date.setText(review.getDate());

				TextView body = (TextView) reviewView.findViewById(R.id.review_body);
				body.setText(review.getBody());

				TextView rating = (TextView) reviewView.findViewById(R.id.review_rating);
				rating.setText(review.getRating());

				reviewList.addView(reviewView);
			}
		}
	}

	private void registerReviewButton() {
		Button reviewButton = (Button) findViewById(R.id.rate_and_review_button);
		reviewButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ReviewDialog reviewDialog = new ReviewDialog(GameView.this, gameInfo.getName());
				reviewDialog.show();
				reviewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
					@Override
					public void onDismiss(DialogInterface dialog) {
						GameView.this.recreate();
					}
				});
				
			}
		});
	}
}
